package net.zjitc.controller;

import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/index")
    public String index(){
        return "index";
    }
    @RequestMapping(value = "/findAll")
    public String findAll(Model model){
        List<User> all = userService.findAll();
        model.addAttribute("list",all);
        return "";
    }
    @RequestMapping(value = "/loginUI")
    public String loginUI(){
        return "login";
    }
    @RequestMapping(value = "/login")
    public String login(Model model, @RequestParam Map map, HttpSession session){
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        User user1 = new User(username, password);
        User user = userService.check_login(user1);
        if(user!=null){
            model.addAttribute("msg","登录成功");
            System.out.println(user);
            session.setAttribute("user",user);
            return "msg";
        }
        model.addAttribute("msg","登录失败");
        return "msg";
    }
    @RequestMapping(value = "/return_login")
    public String return_login(HttpSession session){
        session.removeAttribute("user");
        return "/index";
    }
    @RequestMapping(value = "/registUI")
    public String register(){
        return "regist";
    }
    @RequestMapping(value = "/regist")
    public String regist(Model model,User user){
        userService.insert(user);
        model.addAttribute("msg","注册成功");
        return "msg";
    }
    @RequestMapping(value = "/modifyUI")
    public String forgetUI(User user,HttpSession session,Model model){
        System.out.println(user);
        User user1 = userService.findByID(user);
        model.addAttribute("student",user1);
        return "modify";
    }
    @RequestMapping(value = "/modify")
    public String modify(User user,Model model){
        System.out.println(user);
        userService.update(user);
        User byID = userService.findByID(user);
        model.addAttribute("student",byID);
        return "userinfo";
    }
}

